package com.lfy.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.lfy.pay.zfb.AlipayApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class TestaController {

    @RequestMapping("/pay/test")
    public String test() {
        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel  model = new AlipayTradePagePayModel ();
        // 设置商户订单号
        model.setOutTradeNo("20150320010101001");

        // 设置订单总金额
        model.setTotalAmount("88.88");

        // 设置订单标题
        model.setSubject("Iphone6 16G");

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
//        // 设置PC扫码支付的方式
//        model.setQrPayMode("2");
//        // 设置卖家支付宝用户ID
//        model.setSellerId("2088721035708960");

        // 设置订单附加信息
        model.setBody("Iphone6 16G");
        model.setIntegrationType("PCWEB");
        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        model.setExtendParams(extendParams);
//
//        // 设置商户传入业务信息
//        BusinessParams businessParams = new BusinessParams();
//        businessParams.setMcCreateTradeIp("127.0.0.1");
//        model.setBusinessParams(businessParams);

//        // 设置可打折金额
//        model.setDiscountableAmount("80.00");
//
//        // 设置商户门店编号
//        model.setStoreId("NJ_001");
//
//        // 设置商户操作员编号
//        model.setOperatorId("yx_001");
//
//        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);

        ExtUserInfo extUserInfo = new ExtUserInfo();
        extUserInfo.setCertType("IDENTITY_CARD");
        extUserInfo.setCertNo("2088721035708960");
        extUserInfo.setName("吕飞扬");
        extUserInfo.setMobile("16587658765");
        extUserInfo.setMinAge("24");
        extUserInfo.setNeedCheckInfo("F");
        extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");
        model.setExtUserInfo(extUserInfo);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePagePayResponse response = null;
        try {
            response = AlipayApi.pageExecute(request,"GET");
        } catch (AlipayApiException e) {
            log.error("调用支付宝接口失败", e);
        }
        log.info("response: {}", response);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
             String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
             System.out.println(diagnosisUrl);
            System.out.println("调用失败");

        }
        return response.getBody();
    }
    @RequestMapping("/pay/test2")

    public String test2() throws AlipayApiException {
        // 构造请求参数以调用接口

        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        // 设置商户订单号
        model.setOutTradeNo("20150320010101001");

        // 设置订单总金额
        model.setTotalAmount("88.88");

        // 设置订单标题
        model.setSubject("Iphone6 16G");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721035708960");
/*
        // 设置订单附加信息
        model.setBody("Iphone6 16G");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setSpecifiedSellerName("XXX的跨境小铺");
        extendParams.setCardType("S0JP0000");
        model.setExtendParams(extendParams);

        // 设置商户传入业务信息
        BusinessParams businessParams = new BusinessParams();
        businessParams.setMcCreateTradeIp("127.0.0.1");
        model.setBusinessParams(businessParams);

        // 设置可打折金额
        model.setDiscountableAmount("80.00");

        // 设置商户门店编号
        model.setStoreId("NJ_001");

        // 设置商户操作员编号
        model.setOperatorId("yx_001");

        // 设置商户机具终端编号
        model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");*/


        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = AlipayApi.imageCreatePayToResponse(model);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response.getBody();
    }
}
